package com.example.wangyuyuan.androidframe.utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;

/**
 * Created by wangyuyuan on 2017/5/9.
 */

public class PermissionUtil {
    /**
     * 申请相机权限
     *
     * @param activity Activity
     * @param code     code
     */
    public static void applyCameraPermission(Activity activity, int code) {
        //没有相机权限，申请权限，
        ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.CAMERA},
                code);
    }

    /**
     * 申请SDCardWrite权限
     *
     * @param activity Activity
     * @param code     code
     */
    public static void applySDCardWritePermission(Activity activity, int code) {
        ActivityCompat.requestPermissions(activity,
                new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, code);
    }

    /**
     * 申请SDCardWrite权限
     *
     * @param activity Activity
     * @param code     code
     */
    public static void aplySDCardReadPermission(Activity activity, int code) {
        ActivityCompat.requestPermissions(activity,
                new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, code);
    }

    /**
     * 申请SDCardWrite权限
     *
     * @param activity Activity
     * @param code     code
     */
    public static void applyGPSPermission(Activity activity, int code) {
        ActivityCompat.requestPermissions(activity,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, code);
    }

    //检查相机权限
    public static boolean checkCameraPermission(Context context) {
        return ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) ==
                PackageManager.PERMISSION_GRANTED;
    }

    //检查WriteSDCard权限
    public static boolean checkWriteSDCardPermission(Context context) {
        return ContextCompat.checkSelfPermission(context,
                Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
    }

    //检查ReadSDCard权限
    public static boolean checkReadSDCardPermission(Context context) {
        return ContextCompat.checkSelfPermission(context,
                Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
    }

    //检查定位权限
    public static boolean checkGPSPermission(Context context) {
        return ContextCompat.checkSelfPermission(context,
                Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
    }

    //检查安装程序权限
    public static boolean checkInstallPermission(Context context) {
        return ContextCompat.checkSelfPermission(context, Manifest.permission.INSTALL_PACKAGES) ==
                PackageManager.PERMISSION_GRANTED;
    }

    /**
     * 申请安装程序权限
     *
     * @param activity Activity
     * @param code     code
     */
    public static void applyInstallPermission(Activity activity, int code) {
        ActivityCompat.requestPermissions(activity,
                new String[]{Manifest.permission.INSTALL_PACKAGES}, code);
    }
}
